<?php
include_once("../../configure/configure.php");		//--> global var
include_once("../configure/admin.config.inc.php");	//--> admin global var

include_once("Smarty.class.php");					//--> out template
include_once("impl/AuthImpl.php");					//--> Authority
include_once("impl/OperateImpl.php");				//--> Operation
include_once("impl/UserGroupImpl.php");				//--> User Group

require_once("controlHeader.php");					//--> system control header


$authImpl 		= new AuthImpl(DB_TAG_SYSTEM);		//--> Authority
$operateImpl 	= new OperateImpl(DB_TAG_SYSTEM);	//--> Operation
$userGroupImpl 	= new UserGroupImpl(DB_TAG_SYSTEM);	//--> User Group

if(isset($_POST['groupid']) && !empty($_POST['groupid'])){
	$groupid = $_POST['groupid'];
}else {
	$groupid = 0;
}


$error_message = '';

if(isset($_POST['Submit'])){
	$arrOperation 	= $_POST['arrOperation'];
	$groupid		= $_POST['groupid'];
	
	if(is_array($arrOperation)){
		$arrOperateCache = array();
		foreach ($arrOperation as $value){
			$arrCache = explode('_', $value);//-- 0: menuid, 1: operate id
			$arrOperateCache[$arrCache[0]][] = $arrCache[1];
		}
		
		$isReturn = $authImpl->addGroupAuthTransaction($groupid, $arrOperateCache);
		if($isReturn)
			$error_message = 'add successfully.';
		else 
			$error_message = 'add failure.';
	}
}

$arrOperateList 	= $operateImpl->listOperation();
$arrPermissionList 	= $authImpl->listGroupPermission($groupid);

if(is_array($arrPermissionList)){
	foreach ($arrPermissionList as $key => $arrPer){
		$arrOperate = explode(',', $arrPer['operate']);
		
		$arrOperateCache = array();
		foreach ($arrOperateList['id'] as $keyid => $arrID){
			$arrOperateCache[$keyid] = $arrPer['mid'] . '_' . $arrID;
		}
		foreach ($arrOperate as $keyid1 => $arrID1){
			$arrOperate[$keyid1] = $arrPer['authMenuID'] . '_' . $arrID1;
		}
		
		$arrPermissionList[$key]['arrOperationID'] 		= $arrOperateCache;
		$arrPermissionList[$key]['arrOperationName'] 	= $arrOperateList['name'];
		$arrPermissionList[$key]['arrOperationS'] 		= $arrOperate;
	}
}

$arrGroupID = $userGroupImpl->getGroupSelect($gid);

/*----- out html -----*/
$smarty = new Smarty();	//-- out template
$smarty->template_dir = TEMPLATE_SYS_DIR;
$smarty->compile_dir  = CACHE_SYS_DIR;

$smarty->assign('error_message', $error_message);
$smarty->assign('arrGroupID', $arrGroupID);
$smarty->assign('groupid', $groupid);

$smarty->assign('arrPermissionList', $arrPermissionList);

$smarty->display('GroupPermission.htm');
?>
